<template>
	<view class="page group-introduction-edit">
		<nav-header :title="title" :show-right="false">
		</nav-header>

		<view class="introduction-title">
			<text class="text-sm-Medium" v-if="type=='qun'">编辑群聊介绍</text>
			<text class="text-sm-Medium" v-else>编辑企业信息</text>
			<view class="text-sm-Regular counter">
				{{countContent}}<text style="color: #A4A7AE;">/300</text>
			</view>
		</view>

		<view class="introduction-edit">
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-forms-item name="content">
					<uni-easyinput type="textarea" @input="sumFontNum" autoHeight maxlength="300"
						v-model="formData.content" :inputBorder="false" placeholder="请输入内容" />
				</uni-forms-item>
			</uni-forms>
		</view>

		<view class="tabbar">
			<view class="list">
				<view class="right">
					<button class="btncolor" type="primary" size="mini" @tap.stop.native="upGroupIntro">
						<text class="text-md-Medium">保存</text>
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					content: ""
				},
				rules: {
					content: {
						rules: [{
							required: true,
							errorMessage: '请填写内容',
						}, ]
					}
				},
				groupId: null,
				type: null, //判断是群介绍还是企业信息
				countContent: 0
			}
		},
		methods: {
			sumFontNum(e) {
				this.countContent = e.length
			},
			loadGroupInfo() {
				if (this.type == 'qun') {
					this.$http({
						url: `/group/find/${this.groupId}`,
						method: 'GET'
					}).then((group) => {
						this.formData.content = group.groupIntro;
						this.countContent = group.groupIntro.length
					});
				}
				if (this.type == 'company') {
					this.$http({
						url: "/company/getCompanyInfo",
						method: "POST",
						data: {
							groupId: this.groupId
						}
					}).then((company) => {
						this.formData.content = company.profile;
						this.countContent = company.profile.length
					})
				}
			},
			upGroupIntro() {
				if(this.formData.content == ""){
					uni.showToast({
						title:"内容不能为空",
						icon:"error"
					})
					return
				}
				if (this.type == 'qun') {
					let data = {
						id: this.groupId,
						groupIntro: this.formData.content
					}
					this.$http({
						url: "/group/modify",
						method: "PUT",
						data: data
					}).then((data) => {
						uni.showToast({
							title: `修改成功`,
							icon: 'success'
						})
						setTimeout(() => {
							// 回退并刷新
							let pages = getCurrentPages();
							let prevPage = pages[pages.length - 2];
							prevPage.$vm.loadGroupInfo();
							uni.navigateBack();
						}, 1000);
					})
				}
				if (this.type == 'company') {
					this.$http({
						url: "/company/updateCompanyInfo",
						method: "POST",
						data: {
							groupId: this.groupId,
							profile: this.formData.content
						}
					}).then((data) => {
						uni.showToast({
							title: `修改成功`,
							icon: 'success'
						})
						setTimeout(() => {
							// 回退并刷新
							let pages = getCurrentPages();
							let prevPage = pages[pages.length - 2];
							prevPage.$vm.loadGroupInfo();
							uni.navigateBack();
						}, 1000);
					})
				}
			}
		},
		computed: {
			title() {
				if (this.type == "qun") {
					return "群介绍"
				}
				if (this.type == "company") {
					return "企业信息"
				}
			}
		},
		onLoad(options) {
			this.groupId = options.id;
			this.type = options.type
			// 查询群聊信息
			this.loadGroupInfo(options.id);
		}
	}
</script>

<style lang="scss" scoped>
	.group-introduction-edit {
		.introduction-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			color: $gray-900;
			padding: 30rpx;

			.counter {
				color: $brand-500;
			}
		}

		.introduction-edit {
			padding: 0 30rpx;

			:deep(.uni-forms) {
				height: 232rpx;
			}

			:deep(.uni-easyinput__content.is-textarea) {
				// height: 40rpx;
				padding: 24rpx 32rpx;
				background-color: $gray-100 !important;
			}

		}

		.tabbar {
			width: 100%;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1rpx solid $gray-200;
			background-color: $white;
			z-index: 100;

			.list {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 24rpx 32rpx;

				.right {
					width: 100%;

					.btncolor {
						width: 100%;
						background-color: $brand-500;
						color: $white;
						padding: 16rpx 32rpx;
						border-radius: 40rpx;
						text-align: center;
						line-height: 46rpx;
						vertical-align: middle;
					}
				}
			}
		}
	}
</style>